'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, TEXT } = app.Sequelize;

  const Category = app.model.define('Category', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    type: INTEGER, // 1 办公用品 2 大桶水
    name: STRING(100),
    parent_id: { type: INTEGER, defaultValue: 0 },
    sort: { type: INTEGER, defaultValue: 0 },
    status: { type: INTEGER, defaultValue: 1 },
    create_time: DATE,
    update_time: DATE,
    icon: STRING(255)
  },{
    tableName: 'category',
    timestamps: true, // 启用时间戳(createdAt, updatedAt)
    updatedAt: 'update_time', // 不想要 updatedAt
    createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 create_time
  });

  Category.associate = function() {
    app.model.Category.hasMany(app.model.Category, { foreignKey: 'parent_id', as: 'children' });
    app.model.Category.belongsTo(app.model.Category, { foreignKey: 'parent_id', as: 'parent' });
  };

  return Category;
};
